// pages/manage_shop/manage_shop.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    datas: [],
    isLoadMore: false,
    total_page: 0,
    page: 1,
    imgPath: app.globalData.imgPath
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取数据
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取数据
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.micro.frontend.controllers.MicroShop.get-goods.index');
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.total_page = res.data.list.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          that.setData({
            datas: res.data.list.data,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      urlStr = app.getNetAddresss('plugin.micro.frontend.controllers.MicroShop.get-goods.index');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page
        },
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              datas: this.data.datas.concat(res.data.list.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  delteteItem(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    let that = this;
    let urlStr = app.getNetAddresss('plugin.micro.frontend.controllers.MicroShop.del-goods.index');
    urlStr += '&id=' + item.id;
    wx.showModal({
      title: '提示',
      content: '是否删除该商品？',
      success(res) {
        if (res.confirm) {
          app._getNetWork({
            url: urlStr,
            success: (resdata)=> {
              var res = resdata.data;
              if (res.result == 1) {
                let datas = that.data.datas;
                datas.splice(index, 1);
                that.setData({
                  datas: datas
                });
                wx.showToast({
                  icon: 'none',
                  title: '操作成功',
                  duration: 1500
                });
              } else {
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });
              }
            },
            fail: function (res) {
              console.log(res);
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },
  toItem(e) {
    let goods = e.currentTarget.dataset.goods;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + goods.id
    });
  }
});